/*:
 * @plugindesc (v1.02)地圖-方向指針
 * @author 小c
 * @version 1.00
 * @date 12/6/2021
 *
 * @param Standard
 * @text ===標準點====
 *
 * @param Standard X
 * @text 標準點X座標
 * @parent Standard
 * @default $gamePlayer.screenX()
 *
 * @param Standard Y
 * @text 標準點Y座標
 * @parent Standard
 * @default $gamePlayer.screenY()
 *
 * @param Cursor
 * @text 指針
 *
 * @param R Cursor
 * @text 右向指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param RD Cursor
 * @text 右下指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param D Cursor
 * @text 下向指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param LD Cursor
 * @text 左下指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param L Cursor
 * @text 左向指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param LU Cursor
 * @text 左上指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param U Cursor
 * @text 上向指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @param RU Cursor
 * @text 右上指針
 * @desc 此參數需配置一個有效的滑鼠指針狀態名
 * @parent Cursor
 *
 * @help 
 * 此插件僅在地圖畫面生效
 * 方向指針，根據你滑鼠與你主角的相對方向決定滑鼠樣式
 * 比如滑鼠在玩家角色正上方，滑鼠將變為上向指針
 * 判斷方向的標準點也可以修改，默認是角色當前屏幕座標
 * 可以根據需要修改
 * 此插件需要CP_MouseMove和CP_CustomMouseCursor支持，請放在它們下面
 * 若使用了CP_X_EventMouseCursor，請放在它的上面
 *
 * 若進入了地圖事件，滑鼠將恢復為地圖默認效果。
 * 
 * 更新日誌
 * v1.00
 * 插件完成
 *
 * v1.01
 * 支持標準點座標的修改
 *
 * v1.02
 * 此插件可不依賴CP_MouseMove運行
 */

var Imported = Imported || {};
Imported.CP_X_DirectionMouseCursor = true;

var CP = CP || {};
CP.DirectionMouseCursor = CP.DirectionMouseCursor || {};
CP.DirectionMouseCursor.Params = PluginManager.parameters("CP_X_DirectionMouseCursor");

CP.DirectionMouseCursor.STANDARD_X = CP.DirectionMouseCursor.Params["Standard X"];
CP.DirectionMouseCursor.STANDARD_Y = CP.DirectionMouseCursor.Params["Standard Y"];

CP.DirectionMouseCursor.DIRECTION_DATA = {
	r: {
		cursor: CP.DirectionMouseCursor.Params["R Cursor"],
		sin: {min: -Math.sin(Math.PI / 8), max: Math.sin(Math.PI / 8), equalMin: true, equalMax: false},
		cos: {min: Math.cos(Math.PI / 8), max: 1, equalMin: true, equalMax: true}
	},
	rd: {
		cursor: CP.DirectionMouseCursor.Params["RD Cursor"],
		sin: {min: Math.sin(Math.PI / 8), max: Math.sin(3 * Math.PI / 8), equalMin: true, equalMax: false},
		cos: {min: Math.cos(3 *Math.PI / 8), max: Math.cos(Math.PI / 8), equalMin: false, equalMax: true}
	},
	d: {
		cursor: CP.DirectionMouseCursor.Params["D Cursor"],
		sin: {min: Math.sin(3 * Math.PI / 8), max: 1, equalMin: true, equalMax: true},
		cos: {min: -Math.cos(3 * Math.PI / 8), max: Math.cos(3 * Math.PI / 8), equalMin: false, equalMax: true}
	},
	ld: {
		cursor: CP.DirectionMouseCursor.Params["LD Cursor"],
		sin: {min: Math.sin(Math.PI / 8), max: Math.sin(3 * Math.PI / 8), equalMin: false, equalMax: true},
		cos: {min: -Math.cos(Math.PI / 8), max: -Math.cos(3 * Math.PI / 8), equalMin: false, equalMax: true}
	},
	l: {
		cursor: CP.DirectionMouseCursor.Params["L Cursor"],
		sin: {min: -Math.sin(Math.PI / 8), max: Math.sin(Math.PI / 8), equalMin: false, equalMax: true},
		cos: {min: -1, max: -Math.cos(Math.PI / 8), equalMin: true, equalMax: true}
	},
	lu: {
		cursor: CP.DirectionMouseCursor.Params["LU Cursor"],
		sin: {min: -Math.sin(3 * Math.PI / 8), max: -Math.sin(Math.PI / 8), equalMin: false, equalMax: true},
		cos: {min: -Math.cos(Math.PI / 8), max: -Math.cos(3 * Math.PI / 8), equalMin: true, equalMax: false}
	},
	u: {
		cursor: CP.DirectionMouseCursor.Params["U Cursor"],
		sin: {min: -1, max: -Math.sin(3 * Math.PI / 8), equalMin: true, equalMax: true},
		cos: {min: -Math.cos(3 * Math.PI / 8), max: Math.cos(3 * Math.PI / 8), equalMin: true, equalMax: false}
	},
	ru: {
		cursor: CP.DirectionMouseCursor.Params["RU Cursor"],
		sin: {min: -Math.sin(3 * Math.PI / 8), max: -Math.sin(Math.PI / 8), equalMin: true, equalMax: false},
		cos: {min: Math.cos(3 * Math.PI / 8), max: Math.cos(Math.PI / 8), equalMin: true, equalMax: false}
	}
};

//取值是否在區間內
CP.DirectionMouseCursor.isValueInArea = function(value, area){
	if(value > area.max || value < area.min)
		return false;
	if(!area.equalMin && value === area.min)
		return false;
	if(!area.equalMax && value === area.max)
		return false
	return true;
};

CP.DirectionMouseCursor.getDirectionCursor = function(sinValue, cosValue){
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.r.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.r.cos))
		return this.DIRECTION_DATA.r.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.rd.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.rd.cos))
		return this.DIRECTION_DATA.rd.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.d.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.d.cos))
		return this.DIRECTION_DATA.d.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.ld.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.ld.cos))
		return this.DIRECTION_DATA.ld.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.l.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.l.cos))
		return this.DIRECTION_DATA.l.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.lu.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.lu.cos))
		return this.DIRECTION_DATA.lu.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.u.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.u.cos))
		return this.DIRECTION_DATA.u.cursor;
	if(this.isValueInArea(sinValue, this.DIRECTION_DATA.ru.sin) && this.isValueInArea(cosValue, this.DIRECTION_DATA.ru.cos))
		return this.DIRECTION_DATA.ru.cursor;
};

//-----------------
// Game_Map
//-----------------
CP.DirectionMouseCursor.CURRENT_MAP_MOUSE_CURSOR = Game_Map.prototype.currentMouseCursor;
Game_Map.prototype.currentMouseCursor = function(){
	var cursor = this.directionCursor();
	if(!!cursor)
		return cursor;
	return CP.DirectionMouseCursor.CURRENT_MAP_MOUSE_CURSOR.call(this);
};

Game_Map.prototype.directionCursor = function(){
	var dx = CP.CustomMouseCursor.mouseX() - this.playerStandardX();
	var dy = CP.CustomMouseCursor.mouseY() - this.playerStandardY();
	var dr = Math.sqrt(dx * dx + dy * dy);
	if(dr <= 0)
		return null;
	var sinValue = dy / dr;
	var cosValue = dx / dr;
	return CP.DirectionMouseCursor.getDirectionCursor(sinValue, cosValue);
};

Game_Map.prototype.playerStandardX = function(){
	try{
		return eval(CP.DirectionMouseCursor.STANDARD_X);
	}catch(e){
		console.error(e);
		return $gamePlayer.screenX();
	}
};

Game_Map.prototype.playerStandardY = function(){
	try{
		return eval(CP.DirectionMouseCursor.STANDARD_Y);
	}catch(e){
		console.error(e);
		return $gamePlayer.screenY();
	}
};